package com.archermind.style.util;

import com.feng.fengDUtil.FengD;
import com.feng.fengDUtil.Reflections;
import com.feng.fengDUtil.dao.ConnectionManager;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
public class CommonDao extends FengD{

    @Autowired
    DataSource dataSource;

    @PostConstruct
    public void init() {
        this.connectionManager = new ConnectionManager(dataSource);
    }

    public String escapeSQLLike(String likeStr) {
        String str = StringUtils.replace(likeStr, "_", "\\_");
        str = StringUtils.replace(str, "%", "\\%");
        return str;
    }

    public String processLike(String str) {
       return  "%"+escapeSQLLike(str)+"%";
    }



    public  String genePlaceHolders(int length) {
        String placeHolders = String.join(",", Collections.nCopies(length, "?"));
        return placeHolders;
    }


 /*
      public  String formatSqlIn(String input) {
        if(StringUtils.isEmpty(input)){
            return "''";
        }
        StringBuilder sb=new StringBuilder("");
        String[] strs=input.split(",");
        for (int i =0;i<strs.length;i++){
            sb.append("'").append(strs[i]).append("'");
            if(i<strs.length-1){
                sb.append(",");
            }
        }
        return sb.toString();
    }
  public  String formatSqlIn(Collection objs) {
        StringBuilder sb=new StringBuilder("");
        for (Object obj:objs){
            sb.append("'").append(obj).append("',");
        }
        sb.delete(sb.length()-1,sb.length());
        return sb.toString();
    }

    public  String formatSqlIn(Object[] objs) {
        StringBuilder sb=new StringBuilder("");
        for (int i =0;i<objs.length;i++){
            sb.append("'").append(objs[i]).append("'");
            if(i<objs.length-1){
                sb.append(",");
            }
        }
        return sb.toString();
    }*/

    /**
     * 给分页 page的每条记录添加list.
     * 仅限于 page 中 记录 主键为 id,且为长整型.
     */
    public void setSubListForPage(List<Map> list, Class foreignClass, String foreignName, String foreignKey){
        List<Object> foreignKeyList = new ArrayList<>();
        for(Map rec:list){
            if(rec.get(foreignKey)!=null){
                foreignKeyList.add(rec.get(foreignKey));
            }
        }
        if(foreignKeyList.size() ==0){
            return;
        }
        List subList = this.selectByIds(foreignClass,foreignKeyList);
        for(Map rec:list){
            for(Object sub:subList){
                if(rec.get(foreignKey).equals(Reflections.invokeGetter(sub, "id"))){
                    rec.put(foreignName,sub);
                }
            }
        }
    }

    public static void main(String[] args) {
        String placeHolders = String.join(",", Collections.nCopies(10, "?"));
        System.out.println(placeHolders);
    }
}
